package com.bawei.room;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/**
 1.mysql:CRUD(增删改查) 后台服务器存储大量数据，需要安装
 2.sqlite:轻量级数据，无需安装----》GreenDao-----》Room(jetpack组件之一)
 3.ORM数据库框架：GreenDao/Room
 ORM：对象关系映射(不需要写sql语句，以对象的形式执行增删改查)

 4。实现流程：
 （1）依赖+配置
 （2）Student:@Entity 表
 （3）StudentDao:@Dao 增删改查
 （4）AppDataBase:@Database 数据库
 （5）activity中执行CRUD操作
 */
public class MainActivity extends AppCompatActivity {
    private StudentDao studentDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        studentDao = AppDataBase.getInstance().getStudentDao();
        //插入一条数据
        {
            Student student = new Student(0,"李绍元",17);
            studentDao.insert(student);
            show();

        }
        //插入多条数据
        {
            Student student1 = new Student(0,"李绍元1",14);
            Student student2 = new Student(0,"李绍元2",19);
            Student student3 = new Student(0,"李绍元3",11);
            ArrayList<Student> students = new ArrayList<>();
            students.add(student1);
            students.add(student2);
            students.add(student3);
            studentDao.insert(students);
            show();
        }

          //删除数据
//        {
//            Student student1 = new Student(1, "", 0);
//            Student student2 = new Student(2, "", 0);
//            ArrayList<Student> students = new ArrayList<>();
//            students.add(student2);
//            students.add(student1);
//            studentDao.delete(students);
//            show();
//        }
        //修改数据
//        {
//            Student student = new Student(3, "李大明白", 24);
//            studentDao.update(student);
//            show();
//        }
        //条件查询
//        show("元");




    }
    //查询所有
    public void show(){
        List<Student> students = studentDao.getAllStudents();
        for (Student student1 : students) {
            Log.d("ytx", "insert: "+student1);
        }

    }
    //模糊查询
    public void show(String name){
        List<Student> students = studentDao.getStudentByName(name);
        for (Student student1 : students) {
            Log.d("ytx", "insert: "+student1);
        }

    }
}